<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd ? '添加商品分类' : '编辑商品分类'"
      :visible.sync="info.isShow"
      center
    >
      <el-form ref="cateinfo" :model="cateinfo" :rules="rules">
        <el-form-item prop="pid" label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="cateinfo.pid" placeholder="请选择上级分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          prop="catename"
          label="分类名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="cateinfo.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <!--  -->
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="onRemove"
            :on-change="onChange"
            :auto-upload="false"
            :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cateinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" @click="confirm" type="primary"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="editMenu">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { cateAdd, cateEdit, cateInfo } from "../../request/api";
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      filelist: [],
      cateinfo: {
        pid: 0,
        catename: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      rules: {
        catename: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        id: [{ required: true, message: "请选择商品", trigger: "change" }],
      },
    };
  },
  props: ["info"],
  computed: {
    ...mapGetters({
      cateList: "cate/getCate",
    }),
  },
  methods: {
    ...mapActions({
      getCate: "cate/getCateActions",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    onPreview(file) {
      //封装一个图片预览
      console.log(file, "file");
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    onRemove() {
      //封装图片删除
    },
    onChange(file, fileList) {
      console.log(file, "file");
      console.log(fileList, "fileList");
      this.cateinfo.img = file.raw;
    },
    editMenu() {
      this.$refs.cateinfo.validate((valid) => {
        if (valid) {
          cateEdit(this.cateinfo).then((res) => {
            console.log(res, "添加分类");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getCate();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    reset() {
      this.cateinfo = {
        id: "",
        catename: "",
        img: "",
        status: 1,
      };
      this.filelist = [];
      this.$refs.cateinfo.resetFields(); //清空验证
    },
    confirm() {
      //添加
      this.$refs.cateinfo.validate((val) => {
        if (val) {
          if (this.info.isAdd) {
            cateAdd(this.cateinfo).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getCate();
                this.cancel();
              } else {
                this.$message.error(res.msg);
                return false;
              }
            });
          }
        }
      });
    },
    lookup(id) {
      cateInfo({ id }).then((res) => {
        if (res.code == 200) {
          this.cateinfo = res.list;
          this.cateinfo.id = id;
          this.filelist = [{ url: this.$http + this.cateinfo.img }];
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
